package com.caigou.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {
    private static Logger log = LoggerFactory.getLogger(Md5Util.class);

	/**Determine encrypt algorithm MD5*/
	private static final String ALGORITHM_MD5 = "MD5";
	/**UTF-8 Encoding*/
	private static final String UTF_8 = "UTF-8";

	/**
	 * MD5 16bit Encrypt Methods.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
	public static final String MD5_16bit(String readyEncryptStr, String encoding){
		if(readyEncryptStr != null){
			return Md5Util.MD5_32bit(readyEncryptStr, encoding).substring(8, 24);
		}else{
			return null;
		}
	}

	/**
	 * MD5 32bit Encrypt Methods.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
	public static final String MD5_32bit(String readyEncryptStr, String encoding){
		if(readyEncryptStr != null){
			try {
				//Get MD5 digest algorithm's MessageDigest's instance.
				MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
				//Use specified byte update digest.
				md.update(readyEncryptStr.getBytes(encoding));
				//Get cipher text
				byte [] b = md.digest();
				//The cipher text converted to hexadecimal string
				StringBuilder su = new StringBuilder();
				//byte array switch hexadecimal number.
				for(int offset = 0,bLen = b.length; offset < bLen; offset++){
					String haxHex = Integer.toHexString(b[offset] & 0xFF);
					if(haxHex.length() < 2){
						su.append("0");
					}
					su.append(haxHex);
				}
				return su.toString();
			} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				log.error(e.getMessage(), e);
				return null;
			}
		}else{
			return null;
		}
	}

	/**
	 * MD5 32bit Encrypt Methods.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
	public static final String MD5_32bit1(String readyEncryptStr, String encoding){
		if(readyEncryptStr != null){
			try {
				//The cipher text converted to hexadecimal string
				StringBuilder su = new StringBuilder();
				//Get MD5 digest algorithm's MessageDigest's instance.
				MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
				byte [] b = md.digest(readyEncryptStr.getBytes(encoding));
				int temp = 0;
				//byte array switch hexadecimal number.
				for(int offset = 0,bLen = b.length; offset < bLen; offset++){
					temp = b[offset];
					if(temp < 0){
						temp += 256;
					}
			        int d1 = temp / 16;
			        int d2 = temp % 16;
			        su.append(Integer.toHexString(d1) + Integer.toHexString(d2)) ;
				}
				return su.toString();
			} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}else{
			return null;
		}
	}

	/**
	 * MD5 32bit Encrypt Methods.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
	public static final String MD5_32bit2(String readyEncryptStr, String encoding){
		if(readyEncryptStr != null){
			try {
				//The cipher text converted to hexadecimal string
				StringBuilder su = new StringBuilder();
				//Get MD5 digest algorithm's MessageDigest's instance.
				MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
				//Use specified byte update digest.
				md.update(readyEncryptStr.getBytes(encoding));
				byte [] b = md.digest();
				int temp = 0;
				//byte array switch hexadecimal number.
				for(int offset = 0,bLen = b.length; offset < bLen; offset++){
					temp = b[offset];
					if(temp < 0){
						temp += 256;
					}
					if(temp < 16){
						su.append("0");
					}
					su.append(Integer.toHexString(temp));
				}
				return su.toString();
			} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}else{
			return null;
		}
	}

	/**
	 * MD5 64bit Encrypt Methods.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
//	public static final String MD5_64bit(String readyEncryptStr) throws NoSuchAlgorithmException, UnsupportedEncodingException{
//		MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
//		BASE64Encoder base64Encoder = new BASE64Encoder();
//		return base64Encoder.encode(md.digest(readyEncryptStr.getBytes(UTF-8)));
//
//	}

	/**
	 * MD5 64bit Encrypt Methods2.
	 * @param readyEncryptStr ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 * */
	public static final String MD5_64bit(String readyEncryptStr, String encoding){
		try {
			MessageDigest md;
			md = MessageDigest.getInstance(ALGORITHM_MD5);
			byte[] bytes;
			bytes = md.digest(readyEncryptStr.getBytes(encoding));
			return Base64.encodeBase64String(bytes);
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}


public static void main(String[] args) {
		try {
			//String str = "123456hes_es_wang@hnair.com14951495777335060e3y28e2hf*(eHwHh";
			String str = "3297676hes_wang@hnair.net1535525124304e3y28e2hf*(eHwHh";//123456";
			String md516 = Md5Util.MD5_16bit(str, "UTF-8");
			System.out.println("16bit-md5:\n" + md516); //
			String md532 = Md5Util.MD5_32bit(str, "UTF-8");
			String md5321 = Md5Util.MD5_32bit1(str, "UTF-8");
			String md5322 = Md5Util.MD5_32bit2(str, "UTF-8");
			System.out.println("32bit-md5:"); //5d052f1e32af4e4ac2544a5fc2a9b992
			System.out.println("1:  " + md532);
			System.out.println("2:  " + md5321);
			System.out.println("3:  " + md5322);
			
			String md564 = Md5Util.MD5_64bit(str, "UTF-8");
			System.out.println("64bit-md5:\n" + md564+", length="+md564.length()); //
			String md5642 = Md5Util.MD5_64bit("kaka123", "UTF-8");
			System.out.println("64bit-md52:\n" + md5642+", length="+md5642.length()); //
			
			String sss ="wang@hnair.com";
			System.out.println(sss.substring(0, sss.indexOf("@")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
